package personalPage.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;
import com.makeramen.roundedimageview.RoundedImageView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.BaseActivity;
import common.Constant;
import https.InterNetController;
import personalPage.bean.PersonalPageBean;
import utils.MyLogUtils;
import utils.ShowImageUtils;
import view.MyImageLoader;

/**
 * Created by Administrator on 2017/4/6.
 * 个人首页（别人看）
 */

@ContentView(R.layout.personal_page)
public class PersonalPage extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.personalPage_back)
    private ImageView personalPage_back;
    @ViewInject(R.id.personalPage_headima)
    private RoundedImageView personalPage_headima;
    @ViewInject(R.id.personalPage_name)
    private TextView personalPage_name;
    @ViewInject(R.id.personalPage_detail)
    private TextView personalPage_detail;
    @ViewInject(R.id.personalPage_grade)
    private TextView personalPage_grade;
    @ViewInject(R.id.personalPage_questionNum)
    private TextView personalPage_questionNum;
    @ViewInject(R.id.personalPage_collegeNum)
    private TextView personalPage_collegeNum;
    @ViewInject(R.id.personalPage_volunteerNum)
    private TextView personalPage_volunteerNum;
    @ViewInject(R.id.personalPage_questionLayout)
    private RelativeLayout personalPage_questionLayout;
    @ViewInject(R.id.personalPage_collegeLayout)
    private RelativeLayout personalPage_collegeLayout;
    @ViewInject(R.id.personalPage_volunteerLayout)
    private RelativeLayout personalPage_volunteerLayout;

    private PersonalPageBean personalPageBean;
    private PersonalPageBean.UserBean user;
    private String userId;
    List<String> list;

    private String from;//微信还是app用户 wx/app


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    Gson gson = new Gson();
                    personalPageBean = gson.fromJson(msg.obj.toString(), PersonalPageBean.class);
                    if (personalPageBean != null) {
                        user = personalPageBean.getUser();
                        if (user != null) {
                            MyImageLoader.showImage(user.getHeadimgurl(), personalPage_headima);
                            personalPage_name.setText(user.getName());
                            personalPage_detail.setText(user.getSchool_prov() + "/" + user.getType());
                            personalPage_grade.setText("成绩 : " + user.getCount() + "分");
                            personalPage_questionNum.setText(user.getQuestionCount());
                            personalPage_collegeNum.setText(user.getCollegeCount());
                            personalPage_volunteerNum.setText(user.getExpertCount());
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        userId = getIntent().getStringExtra("userId");
        from = getIntent().getStringExtra("type");
        initView();
        getDataFromInterNet();
    }

    private void initView() {
        personalPage_back.setOnClickListener(this);
        personalPage_headima.setOnClickListener(this);
        personalPage_questionLayout.setOnClickListener(this);
        personalPage_collegeLayout.setOnClickListener(this);
        personalPage_volunteerLayout.setOnClickListener(this);
        list = new ArrayList<>();
    }


    @Override
    public void onClick(View v) {
        Intent i = new Intent();
        switch (v.getId()) {
            case R.id.personalPage_back:
                PersonalPage.this.finish();
                break;
            case R.id.personalPage_headima:
                list.clear();
                list.add(user.getHeadimgurl());
                ShowImageUtils showImageUtils = new ShowImageUtils();
                showImageUtils.showImageUtils(this, personalPage_headima, list, 0);
                break;
            case R.id.personalPage_questionLayout:
                //问题列表
                i.setClass(this, QuestionList.class);
                i.putExtra("userId", userId);
                i.putExtra("from", from);

                startActivity(i);
                break;
            case R.id.personalPage_collegeLayout:
                //高校列表
                i.setClass(this, CollegeList.class);
                i.putExtra("userId", userId);
                i.putExtra("from", from);
                startActivity(i);
                break;
            case R.id.personalPage_volunteerLayout:
                //志愿者列表
                i.setClass(this, VolunteerList.class);
                i.putExtra("userId", userId);
                i.putExtra("from", from);
                startActivity(i);
                break;
            default:
                break;
        }
    }

    private void getDataFromInterNet() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("user_id", userId);
        hashMap.put("from", from);
        new InterNetController(this, Constant.HOMEPAGE, handler, hashMap, 0);
    }
}
